/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.core.model;

import org.eclipse.core.runtime.IProgressMonitor;

/**
 * Default implementation of the common protocol for all elements provided by
 * the model.
 */
public abstract class AbstractModelElement implements IModelElement {

	/** The parent. */
	private IModelElement parent;

	/** The name. */
	private String name;

	/**
	 * Instantiates a new abstract model element.
	 * 
	 * @param parent
	 *            the parent
	 * @param name
	 *            the name
	 */
	protected AbstractModelElement(IModelElement parent, String name) {
		this.parent = parent;
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	public Object getAdapter(Class adapter) {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.tapestry.ide.core.model.IModelElement#getElementParent()
	 */
	public IModelElement getElementParent() {
		return parent;
	}

	/**
	 * Sets the element parent.
	 * 
	 * @param parent
	 *            the new element parent
	 */
	public void setElementParent(IModelElement parent) {
		this.parent = parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.tapestry.ide.core.model.IModelElement#getElementChildren()
	 */
	public IModelElement[] getElementChildren() {
		return NO_CHILDREN;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.tapestry.ide.core.model.IModelElement#getElementName()
	 */
	public String getElementName() {
		return name;
	}

	/**
	 * Sets the element name.
	 * 
	 * @param name
	 *            the new element name
	 */
	public void setElementName(String name) {
		this.name = name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.tapestry.ide.core.model.IModelElement#accept(com.googlecode
	 * .tapestry.ide.core.model.IModelElementVisitor,
	 * org.eclipse.core.runtime.IProgressMonitor)
	 */
	public void accept(IModelElementVisitor visitor, IProgressMonitor monitor) {

		// Visit only this element
		if (!monitor.isCanceled()) {
			visitor.visit(this, monitor);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof AbstractModelElement)) {
			return false;
		}
		AbstractModelElement that = (AbstractModelElement) other;
		if (this.name == that.name) {
			return true;
		}
		if (this.name == null || that.name == null) {
			return false;
		}
		return this.name.equals(that.name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		if (name == null) {
			return 0;
		}
		return name.hashCode(); // ignore parent
	}
}
